local skynet = require "skynet"
local proto_manager = require "common.proto_manager"
local watchdog_proxy = require "agents.watchdog_proxy"
local player_manager = require "agents.player_manager"

local msg_handler_helper = {}

function msg_handler_helper.parseProtoMessage(fd,t,data)
    local req,err = proto_manager.decode(t,data)
    if err then
        watchdog_proxy.disconnect(fd)
        skynet.error("decode " .. t  .. " err:" .. err .. " fd:" .. fd .. " traceback:" .. debug.traceback())
        return nil
    end
    return req
end

function msg_handler_helper.checkGetPlayerByFd(fd)
    local player = player_manager.getPlayerByFd(fd)
    if not player then
        watchdog_proxy.disconnect(fd)
        skynet.error("not find player! fd:" .. fd .. " traceback:" .. debug.traceback())
        return nil
    end
    return player
end

function msg_handler_helper.checkGetPlayerByOpenID(openid)
    local player = player_manager.getPlayerByOpenID(openid)
    if not player then
        skynet.error("not find player! openid:" .. openid .. " traceback:" .. debug.traceback())
        return nil
    end
    return player
end

return msg_handler_helper